package com.vv.tools.modules.sys.controller;

import com.vv.tools.common.message.Result;
import com.vv.tools.common.validator.AddGroup;
import com.vv.tools.common.validator.UpdateGroup;
import com.vv.tools.modules.sys.converter.MenuConvert;
import com.vv.tools.modules.sys.model.MenuDTO;
import com.vv.tools.modules.sys.service.MenuService;

import jakarta.validation.constraints.NotNull;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

/**
 * <p>
 * 菜单 前端控制器
 * </p>
 *
 * @author vv
 * @since 2025-02-19
 */
@Tag(name = "Menu Controller")
@RestController
@RequestMapping("/sys/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    @Operation(tags = "查询菜单", description = "查询菜单")
    @GetMapping("/{id}")
    public Result<MenuDTO> query(@PathVariable @NotNull(message = "ID不能为空") Long id) {
        return Result.success(MenuConvert.INSTANCE.do2dto(menuService.getById(id)));
    }

    @Operation(tags = "新增菜单", description = "新增菜单")
    @PostMapping
    public Result<Boolean> save(@RequestBody @Validated(value = {AddGroup.class}) MenuDTO dto) {
        return Result.success(menuService.save(MenuConvert.INSTANCE.dto2do(dto)));
    }

    @Operation(tags = "删除菜单", description = "删除菜单")
    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@PathVariable @NotNull(message = "ID不能为空") Long id) {
        return Result.success(menuService.removeById(id));
    }

    @Operation(tags = "修改菜单", description = "修改菜单")
    @PutMapping
    public Result<Boolean> update(@RequestBody @Validated(value = {UpdateGroup.class}) MenuDTO dto) {
        return Result.success(menuService.updateById(MenuConvert.INSTANCE.dto2do(dto)));
    }

    @Operation(tags = "查询所有菜单", description = "查询所有菜单")
    @GetMapping("")
    public Result<List<MenuDTO>> getAllMenus() {
        return Result.success(menuService.getAllMenus());
    }

}
